package com.icow.wms.basic.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.icow.wms.basic.domain.Vehicle;
import com.icow.wms.basic.utils.AgvState;
import com.icow.wms.basic.utils.OpenTcs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Service;
import com.icow.wms.basic.mapper.AgvErrorInfoMapper;
import com.icow.wms.basic.domain.AgvErrorInfo;
import com.icow.wms.basic.service.IAgvErrorInfoService;

import javax.annotation.Resource;

/**
 * agvErrorInfoService业务层处理
 *
 * @author bcs
 * @date 2024-08-23
 */
@Service
public class AgvErrorInfoServiceImpl implements IAgvErrorInfoService {
    @Autowired
    private AgvErrorInfoMapper agvErrorInfoMapper;

    private Map<String, String> agvErrorCache = new HashMap<>();

    /**
     * 查询agvErrorInfo
     *
     * @param id agvErrorInfoID
     * @return agvErrorInfo
     */
    @Override
    public AgvErrorInfo selectAgvErrorInfoById(Long id) {
        return agvErrorInfoMapper.selectAgvErrorInfoById(id);
    }

    @Override
    public void monitorVehicleErrorInfo(Vehicle vehicle) {
        if (agvErrorCache.get(vehicle.getName()) == null && AgvState.ERROR.equals(vehicle.getState())) {
            String UUIDOnly = UUID.randomUUID().toString().substring(0, 8);
            AgvErrorInfo agvErrorInfo = new AgvErrorInfo();
            agvErrorInfo.setAgvName(vehicle.getName());
            agvErrorInfo.setOrderName(vehicle.getTransportOrder() == null ? "无任务" : vehicle.getTransportOrder());
            String errorMessage = OpenTcs.getError(OpenTcs.getError1(vehicle), OpenTcs.getError2(vehicle));
            agvErrorInfo.setAgvErrorMessage(errorMessage);
            agvErrorInfo.setUniqueCode(UUIDOnly);
            agvErrorInfo.setErrorTime(new Date());
            agvErrorCache.put(vehicle.getName(), UUIDOnly);
            agvErrorInfoMapper.insertAgvErrorInfo(agvErrorInfo);
        } else if (!AgvState.ERROR.equals(vehicle.getState()) && agvErrorCache.get(vehicle.getName()) != null) {
            String UUIDOnly = agvErrorCache.get(vehicle.getName());
            AgvErrorInfo agvErrorInfo = new AgvErrorInfo();
            agvErrorInfo.setUniqueCode(UUIDOnly);
            List<AgvErrorInfo> agvErrorInfos = selectAgvErrorInfoList(agvErrorInfo);
            if (agvErrorInfos.size() >= 1) {
                agvErrorInfo = agvErrorInfos.get(0);
                agvErrorInfo.setNoErrorTime(new Date());
                agvErrorInfoMapper.updateAgvErrorInfo(agvErrorInfo);
            }
            agvErrorCache.remove(vehicle.getName());
        }
    }

    /**
     * 查询agvErrorInfo列表
     *
     * @param agvErrorInfo agvErrorInfo
     * @return agvErrorInfo
     */
    @Override
    public List<AgvErrorInfo> selectAgvErrorInfoList(AgvErrorInfo agvErrorInfo) {
        return agvErrorInfoMapper.selectAgvErrorInfoList(agvErrorInfo);
    }

    /**
     * 新增agvErrorInfo
     *
     * @param agvErrorInfo agvErrorInfo
     * @return 结果
     */
    @Override
    public int insertAgvErrorInfo(AgvErrorInfo agvErrorInfo) {
        return agvErrorInfoMapper.insertAgvErrorInfo(agvErrorInfo);
    }

    /**
     * 修改agvErrorInfo
     *
     * @param agvErrorInfo agvErrorInfo
     * @return 结果
     */
    @Override
    public int updateAgvErrorInfo(AgvErrorInfo agvErrorInfo) {
        return agvErrorInfoMapper.updateAgvErrorInfo(agvErrorInfo);
    }

    /**
     * 批量删除agvErrorInfo
     *
     * @param ids 需要删除的agvErrorInfoID
     * @return 结果
     */
    @Override
    public int deleteAgvErrorInfoByIds(Long[] ids) {
        return agvErrorInfoMapper.deleteAgvErrorInfoByIds(ids);
    }

    /**
     * 删除agvErrorInfo信息
     *
     * @param id agvErrorInfoID
     * @return 结果
     */
    @Override
    public int deleteAgvErrorInfoById(Long id) {
        return agvErrorInfoMapper.deleteAgvErrorInfoById(id);
    }

    @Override
    public int deleteThirtyDaysAgoData() {
        return agvErrorInfoMapper.deleteThirtyDaysAgoData();
    }
}
